package com.firebird.app.kotlin.stock.trend.view

import android.content.Context
import android.util.AttributeSet
import android.widget.BaseAdapter
import android.widget.LinearLayout

/**
 * 不可滚动的listview，用来解决ListView去ScrollView嵌套时的冲突
 * @author ljk
 * @date   2019年10月29日
 */
class NoScrollListView : LinearLayout {
    private var adapter: BaseAdapter? = null

    //private Context context;
    constructor(context: Context?) : super(context, null) {}

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) { //this.context = context;
    }

    fun getAdapter(): BaseAdapter? {
        return adapter
    }

    /**
     * 设置适配器
     * @param adapter
     */
    fun setAdapter(adapter: BaseAdapter?) {
        this.adapter = adapter
        bindViewInLayout()
    }

    /**
     * 将adapter中的View绑定到LinearLayout中，并显示
     */
    private fun bindViewInLayout() {
        if (adapter != null) {
            val itemCount = adapter!!.count
            for (i in 0 until itemCount) {
                val view = adapter!!.getView(i, null, null)
                addView(view, i)
                view.id = i
            }
        }
    }

    /**
     * 设置每个Item被点击后的事件
     */
    override fun setOnClickListener(listener: OnClickListener) {
        val itemCount = this.childCount
        for (i in 0 until itemCount) {
            val view = getChildAt(i)
            view.setOnClickListener(listener)
        }
    }
}
